﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Infrastructure.Data;
using Keepo.Models;
using Keepo.Helpers;
using System.IO;
using System.Drawing.Imaging;
using AlbumDesigner.Models.Types;
using System.Configuration;
using AlbumDesigner.Migrations;

namespace AlbumDesigner.Controllers
{
    public class HomeController : Controller
    {
        private AlbumContext db = new AlbumContext();

        public ActionResult Index()
        {
            var browser = Request.Browser;

            if (browser.Browser == "IE")
            {
                if (browser.MajorVersion <= 7)
                {
                    return View("UnsupportedBrowser");
                }
            }

            // Page meta
            PageMeta pageMeta = db.PageMetas.First(p => p.Name == "ראשי");
            ViewBag.Description = pageMeta.Description;
            ViewBag.Keywords = pageMeta.Keywords;
            ViewBag.Title = pageMeta.Title;

            return View();
        }

        public ActionResult Fonts()
        {
            return View();
        }

        public ActionResult NoPermission()
        {
            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.ContactMailTypeId = new SelectList(db.ContactMailTypes, "ContactMailTypeId", "Title");

            // Page meta
            PageMeta pageMeta = db.PageMetas.First(p => p.Name == "צור קשר");
            ViewBag.Description = pageMeta.Description;
            ViewBag.Keywords = pageMeta.Keywords;
            ViewBag.Title = pageMeta.Title;

            return View();
        }

        [HttpPost]
        public ActionResult Contact(ContactMail data)
        {
            //insert this contact mail to DB
            db.ContactMails.Add(data);
            db.SaveChanges();

            var contactMailType = db.ContactMailTypes.FirstOrDefault(m => m.ContactMailTypeId == data.ContactMailTypeId);

            var mailTitle = "צור קשר - " + contactMailType.Title;
            var mailText = "<strong>כותרת</strong>: " + data.Title + "<br /><strong>תוכן</strong>: " + StringHelper.ReplaceBR(data.Text) + "<br /><br /><strong>כתובת מייל:</strong> " + data.Email + "<br /><strong>שם:</strong> " + data.Name + "<br /><strong>שם בית ספר:</strong> " + data.SchoolName + "<br /><strong>טלפון:</strong> " + data.Phone;

            AlbumDesigner.Helpers.MailHelper.SendMail("admin@elinirschool.co.il", mailTitle, mailText);

            return Json(new { Key = true, Value = "תודה על פנייתך! אנו נעשה את מירב המאמצים לענות לך בהקדם." });
        }

        public ActionResult TechnicalContact()
        {
            ContactData data = new ContactData();
            data.Email = UserHelper.IsLogon() ? UserHelper.CurrentUser().Email : "";

            return PartialView(data);
        }

        [HttpPost]
        public ActionResult TechnicalContact(ContactData data)
        {
            //insert this contact mail to DB
            var contactMail = new ContactMail() { ContactMailTypeId = 4, Email = data.Email, Text = data.Text, Title = "עזרה טכנית" };
            db.ContactMails.Add(contactMail);
            db.SaveChanges();

            var mailTitle = "עזרה טכנית";
            var mailText = "<strong>תוכן</strong>: " + StringHelper.ReplaceBR(data.Text) + "<br /><br /><strong>כתובת מייל:</strong> " + data.Email;
            string albumLoginUrl = "";

            if (data.AlbumId != null)
            {
                var album = db.Albums.FirstOrDefault(m => m.AlbumId == data.AlbumId);
                mailText += "<br /><strong>אלבום</strong>: " + album.Title;
                albumLoginUrl = Url.Action("EditorLogin", "Users", new { id = album.OrderNum }, "http");
            }

            AlbumDesigner.Helpers.MailHelper.SendMail("support@elinirschool.co.il", mailTitle, mailText, albumLoginUrl);

            return Json(new { Key = true, Value = "תודה על פנייתך! אנו נעשה את מירב המאמצים לענות לך בהקדם." });
        }

        public ActionResult HTCfile()
        {
            //return the HTC behavior file, used for CSS3PIE (CSS3 in IE)
            var pieFile = Server.MapPath("~/Content/PIE/PIE.htc");
            return File(pieFile, "text/x-component");
        }
    }
}
